<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="Cache-Control" content="no-cache" />
        <meta http-equiv="Pragma" content="no-cache" />
        <meta http-equiv="Expires" content="0" />
        <title>CEP13</title>
        <link rel="stylesheet" type="text/css" media="screen" href="../css/stylesheet.css" />
        <script type="text/javascript" src="../js/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="../plugin/fancybox/jquery.fancybox-1.3.4.css" media="screen" />
        <script type="text/javascript" src="../plugin/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
        <script type="text/javascript" src="../plugin/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
        <link rel="stylesheet" type="text/css" href="../plugin/flexigrid/css/flexigrid.css" />
        <script type="text/javascript" src="../plugin/flexigrid/js/flexigrid.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#flex1").flexigrid({
                    url: 'data.php',
                    dataType: 'json',
                    colModel : [
                        {display: 'ID', name : 'content_id', width : 50, sortable : true, align: 'center'},
                        {display: 'Position', name : 'content_position', width : 100, sortable : true, align: 'center'},
                        {display: 'Text', name : 'content_text', width : 830, sortable : true, align: 'left'}
                    ],
                    buttons : [
                        //{name: 'Add', bclass: 'add', onpress : test},
                        //{separator: true},
                        {name: 'Edit', bclass: 'edit', onpress : test},
                        {separator: true}
                        //,{name: 'Delete', bclass: 'delete', onpress : test},
                        //{separator: true}
                    ],
                    searchitems : [
                        {display: 'ID', name : 'content_id', isdefault: true},
                        {display: 'Position', name : 'content_position'}
                    ],
                    sortname: "content_id",
                    sortorder: "asc",
                    usepager: true,
                    title: 'Content',
                    useRp: true,
                    rp: 20,
                    showTableToggleBtn: true,
                    width: 1024,
                    height: 800
                });   
            });

            function test(com,grid) {
                if (com=='Delete') {  // เหตุการณ์เมื่อกดปุ่ม delete 
                    if($('.trSelected',grid).length>0) {
                        if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
                            var items = $('.trSelected',grid);
                            var itemlist ='';
                            for(i=0;i<items.length;i++){
                                itemlist+= items[i].id.substr(3)+",";
                            }
                            $.ajax({
                                type: "POST",
                                dataType: "html",
                                url: "delete.php",
                                data: "items="+itemlist,
                                success: function(data){
                                    alert(data);
                                    $("#flex1").flexReload();
                                }
                            });
                        }
                    } else {
                        alert('กรุณาเลือกข้อมูลที่ต้องการลบ!!');
                    } 
                } else if (com=='Add') {  // เหตุการเมื่อกดปุ่ม add
                    $.fancybox({'href'	: 'add_form.php'});  
                } else if (com=='Edit') { // เหตุการณ์เมื่อกดปุ่ม Edit 
                    var items = $('.trSelected',grid);
                    if(items.length>0) {
                        if( items.length>1) {
                            alert('กรุณาเลือกข้อมูลทีละแถว!!');
                        } else {
                            var id  = items[0].id.substr(3);
                            $.fancybox({'href'	: 'edit_form.php?id='+id});  
                        }
                    } else {
                        alert('กรุณาเลือกข้อมูลที่ต้องการแก้ไข!!');
                    }
                }
            } 
        </script>
    </head>
    <body>
        <?php include('../header.php'); ?>
        <table id="flex1" style="display:none"></table>
    </body>
</html>